document.addEventListener('DOMContentLoaded', function() {
    const captureVisible = document.getElementById('captureVisible');
    const captureFull = document.getElementById('captureFull');
    const captureSelection = document.getElementById('captureSelection');
    const status = document.getElementById('status');

    // 截取可见区域
    captureVisible.addEventListener('click', async() => {
        try {
            const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
            await chrome.scripting.executeScript({
                target: { tabId: tab.id },
                function: captureVisibleArea
            });
        } catch (error) {
            status.textContent = '截图失败：' + error.message;
        }
    });

    // 截取整个页面
    captureFull.addEventListener('click', async() => {
        try {
            const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
            await chrome.scripting.executeScript({
                target: { tabId: tab.id },
                function: captureFullPage
            });
        } catch (error) {
            status.textContent = '截图失败：' + error.message;
        }
    });

    // 选择区域截图
    captureSelection.addEventListener('click', async() => {
        try {
            const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
            await chrome.scripting.executeScript({
                target: { tabId: tab.id },
                function: startSelectionMode
            });
        } catch (error) {
            status.textContent = '截图失败：' + error.message;
        }
    });
});

// 截取可见区域的函数
function captureVisibleArea() {
    chrome.runtime.sendMessage({ action: 'captureVisible' });
}

// 截取整个页面的函数
function captureFullPage() {
    chrome.runtime.sendMessage({ action: 'captureFull' });
}

// 开始选择模式的函数
function startSelectionMode() {
    chrome.runtime.sendMessage({ action: 'startSelection' });
}